package cn.wolfcode.wms.service.impl;


import cn.wolfcode.wms.domain.Employee;
import cn.wolfcode.wms.exception.LogicException;
import cn.wolfcode.wms.mapper.EmployeeMapper;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.EmployeeOueryObject;
import cn.wolfcode.wms.service.IEmployeeService;
import cn.wolfcode.wms.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;

@Service
public class EmployeeService implements IEmployeeService {


    @Autowired
    private EmployeeMapper mapper ;

    public int deleteByPrimaryKey(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    public int insert(Employee record) {
        return mapper.insert(record);
    }

    public Employee selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<Employee> selectAll() {
        return mapper.selectAll();
    }

    public int updateByPrimaryKey(Employee record) {
        return mapper.updateByPrimaryKey(record);
    }

    public void delete(Long id) {

        mapper.deleteByPrimaryKey(id);
    }

    public void saveorupdate(Employee employee, Long[] ids) {

        if(employee.getId() != null){

            mapper.deleteRelation(employee.getId());
            mapper.updateByPrimaryKey(employee);
        }else {
            employee.setPassword(MD5.encode(employee.getPassword()));
            mapper.insert(employee);
        }
        if(ids != null){
            for (Long id : ids) {
                mapper.insertReslation(employee.getId(),id);
            }
        }
    }

    public void deleteById(Long id){
        //维护关系
        mapper.deleteRelation(id);
        mapper.deleteByPrimaryKey(id);
    }





    public PageResult query(EmployeeOueryObject qo) {
        Integer count =  mapper.queryForCount(qo);
        if(count == 0){
            return  PageResult.empty(qo.getPageSize());
        }
          List<Employee> list = mapper.queryForList(qo);
        PageResult pageResult = new PageResult(qo.getPageSize(), count, qo.getCurrentPage(), list);

        return pageResult;
    }

    public void login(Employee emp) {

        //根据用户名查询登录
        Employee empInDB = mapper.selecetByUsername(emp.getName());
        System.out.println(empInDB);

        if(empInDB == null){
            throw  new LogicException("用户名和密码错误");
        }
        //如果用户纯在,在找密码
        String encode = MD5.encode(emp.getPassword());
        System.out.println(encode);
        if(!empInDB.getPassword().equals(encode)){
            throw  new LogicException("用户名和密码错误");
        }
        ServletRequestAttributes  requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        requestAttributes.getRequest().getSession().setAttribute("USER_IN_SESSION",empInDB);


    }

    public void deleteIds(Long[] ids) {

        if(ids != null){
            mapper.deleteByIds(ids);
        }

    }


}
